﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstraX;
using System.ComponentModel.DataAnnotations;
using AbstraX.ServerInterfaces;
using System.ServiceModel.DomainServices.Server;
using System.Runtime.Serialization;
using System.Net;
using System.IO;
using Reflection = System.Reflection;
using System.Diagnostics;
using AbstraX.XPathBuilder;
using System.GAC;
using System.Reflection;
using System.Collections;
using AssemblyProvider.Web.Entities;
using AbstraX.Templates;
using ControlProviders.ProviderInterfaces;
using AbstraX.AssemblyInterfaces;
using AbstraX.Contracts;
using MvvmTreeView;

namespace SLControlProvider.Web.Entities
{
    [DataContract, DebuggerDisplay("{ DebugInfo }"), ClientCodeGeneration(typeof(ControlClientInterfaceGenerator)), KnownType(typeof(Facet))]
    public class SLControlsRoot : IControlsRoot
    {
        private IAssembliesRoot assembliesRoot;

        public SLControlsRoot()
        {
        }
      public IAbstraXExtension LoadExtention(IBase baseObject, ITreeNode treeNode)
        {
            return null;
        }

        public SLControlsRoot(IAssembliesRoot assembliesRoot)
        {
            this.assembliesRoot = assembliesRoot;
        }

        [DataMember]
        public  float ChildOrdinal
        {
            get 
            {
                return assembliesRoot.ChildOrdinal;
            }
        }

        [DataMember]
        public  string DebugInfo
        {
            get 
            {
                return assembliesRoot.DebugInfo;
            }
        }

        [DataMember, Key]
        public  string ID
        {
            get
            {
                return DNSHelper.MakeURL("ControlsRoot");
            }
        }

        [DataMember]
        public  string FolderKeyPair 
        {
            get
            {
                return assembliesRoot.FolderKeyPair;
            }
            set
            {
                assembliesRoot.FolderKeyPair = value;
            }
        }

        [DataMember, Include, Association("SLControlsRoot_SLControlAssemblies", "ID", "ParentID")]
        public List<SLControlAssembly> ControlAssemblies
        {
            get
            {
                List<SLControlAssembly> assemblies = new List<SLControlAssembly>();

                foreach (var assembly in assembliesRoot.Assemblies.Where(a => a.Types.Any(t => t.BaseTypes.Any(b => b.Name == "Control"))))
                {
                    assemblies.Add(new SLControlAssembly(assembly, assembliesRoot));
                }

                return assemblies;
            }
        }

        [DataMember]
        public  string URL
        {
            get 
            {
                return assembliesRoot.URL;
            }
        }

        [DataMember]
        public  ProviderType ProviderType
        {
            get 
            {
                return ProviderType.AssemblyMap;
            }
        }

        [DataMember]
        public  IEnumerable<IElement> RootElements
        {
            get 
            {
                return this.ControlAssemblies;
            }
        }
        
        [DataMember]
        public  string ParentID
        {
            get 
            {
                return assembliesRoot.ParentID;
            }
        }

        [DataMember]
        public  string Name
        {
            get 
            {
                return "Silverlight Controls";
            }
        }

        [DataMember]
        public  string Description
        {
            get
            {
                return assembliesRoot.Description;
            }
        }

        [DataMember]
        public  string Documentation
        {
            get
            {
                return string.Empty;
            }
        }

        [DataMember]
        public  string ImageURL
        {
            get
            {
                return string.Empty;
            }
        }

        [DataMember, Include, Association("Parent_BaseType", "ID", "ParentID")]
        public  BaseType DataType
        {
            get 
            {
                return null;
            }
        }

        [Invoke]
        public  void Dispose()
        {
            assembliesRoot.Dispose();
        }

        public  IBase Parent
        {
            get 
            {
                return assembliesRoot.Parent;
            }
        }

        public  void ExecuteWhere(string property, object value)
        {
            assembliesRoot.ExecuteWhere(property, value);
        }

        public  void ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
            assembliesRoot.ExecuteWhere(expression);
        }

        public  void ExecuteWhere(XPathAxisElement element)
        {
            assembliesRoot.ExecuteWhere(element);
        }

        public  void ClearPredicates()
        {
            assembliesRoot.ClearPredicates();
        }

        public  void ExecuteGlobalWhere(XPathAxisElement element)
        {
            assembliesRoot.ExecuteGlobalWhere(element);
        }

        [DataMember]
        public  DefinitionKind Kind
        {
            get 
            {
                return assembliesRoot.Kind;
            }
        }

        public  string ParentFieldName
        {
            get 
            {
                return assembliesRoot.ParentFieldName;
            }
        }

        [DataMember]
        public  bool HasChildren
        {
            get 
            {
                return assembliesRoot.HasChildren;
            }
        }

        [DataMember, Include, Association("Parent_Facet", "ID", "ParentID")]
        public Facet[] Facets
        {
            get 
            {
                return assembliesRoot.Facets;
            }
        }
    }
}
